home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / gs_cmdl.ps < prev    next >
Text File  |  1996-01-10  |  5KB  |  182 lines

  1. %    Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Parse and execute the command line.
  16. % C code handles the following switches: -h/-? -I -M -v
  17.  
  18. /cmddict 50 dict def
  19. cmddict begin
  20.  
  21. % ---------------- Utility procedures ---------------- %
  22.  
  23. % Get the next argument from the parsed argument list.
  24. /nextarg        % - nextarg <arg> true
  25.             % - nextarg false
  26.  { argv length 0 eq
  27.     { false }
  28.     { argv dup 0 get exch dup length 1 sub 1 exch getinterval /argv exch def }
  29.    ifelse
  30.  } bind def
  31.  
  32. % Run a file, under job control if implemented.
  33. /runjob            % <file> runjob -
  34.  { end        % cmddict
  35.    /startjob where { pop false () startjob pop }
  36.    run
  37.    //cmddict begin
  38.  } bind def
  39. /runfilejob        % <filename> runfilejob -
  40.  { findlibfile { exch pop } { (r) file } runjob
  41.  } bind def
  42.  
  43. % Expand arguments.  Free variables: expand@.
  44. /expandarg        % <string> expandarg <args...>
  45.  { dup () eq
  46.     { pop
  47.     }
  48.     { dup dup (--) eq exch (-+) eq or
  49.        { pop /expand@ false def
  50.        }
  51.        { expand@ { (@) anchorsearch } { false } ifelse
  52.       { pop findlibfile
  53.          { exch pop }
  54.          { (r) file }        % let the error happen
  55.         expandargfile
  56.       }
  57.      if
  58.        }
  59.       ifelse
  60.     }
  61.  } bind def
  62. /expandargfile        % <file> expandargfile <args...>
  63.  { [ exch cvlit
  64.     { token not { exit } if
  65.       dup type /stringtype ne { =string cvs dup length string copy } if
  66.       expandarg
  67.     }
  68.    /exec cvx
  69.    ] cvx loop
  70.  } bind def
  71.  
  72. % ---------------- Recognized switches ---------------- %
  73.  
  74. % Switches with arguments are defined as <x>;
  75. % switches without arguments are defined as -<x>.
  76.  
  77. % Switches without arguments
  78. /--
  79.  { nextarg not
  80.     { (-- and -+ require a file name.\n) print flush }
  81.     { systemdict /ARGUMENTS argv put /argv [] def runjob }
  82.    ifelse
  83.  } bind def
  84. /-+ /-- load def
  85. /-@ /-- load def
  86. /-A { (@) Z } bind def
  87. /-c
  88.  {  { argv length 0 eq { exit } if
  89.       argv 0 get (-) anchorsearch { pop pop exit } if
  90.       pop nextarg token
  91.        { exch pop            % Probably should check for empty.
  92.      end exec //cmddict begin
  93.        }
  94.       if
  95.     }
  96.    loop
  97.  } bind def
  98. /-e { (#) Z } bind def
  99. /-E /-e load def
  100. /-f { } def
  101. /-q { systemdict /QUIET true put } bind def
  102.  
  103. % Switches with arguments
  104. /d
  105.  { (=) search not { (#) search not { () exch dup } if } if
  106.    exch pop cvn dup where
  107.     { pop (Redefining ) print print ( is not allowed.\n) print flush pop }
  108.     { exch token
  109.        { exch pop }        % Probably should check for empty.
  110.        { true }
  111.       ifelse
  112.       systemdict 3 1 roll put
  113.     }
  114.    ifelse
  115.  } bind def
  116. /D /d load def
  117. /f { dup length 0 ne { runfilejob } if } bind def
  118. /g
  119.  { (x) search { cvi pop exch cvi } { cvi dup } ifelse
  120.    systemdict begin /DEVICEHEIGHT exch def /DEVICEWIDTH exch def end
  121.  } bind def
  122. /r
  123.  { (x) search { cvr pop exch cvr } { cvr dup } ifelse
  124.    systemdict begin /DEVICEYRESOLUTION exch def /DEVICEXRESOLUTION exch def end
  125.  } bind def
  126. /s
  127.  { (=) search not { (#) search not { () exch dup } if } if
  128.    exch pop cvn dup where { pop dup load } { () } ifelse
  129.    type /stringtype ne
  130.     { (Redefining ) print print ( is not allowed.\n) print flush pop }
  131.     { exch systemdict 3 1 roll put }
  132.    ifelse
  133.  } bind def
  134. /S /s load def
  135. /Z { true .setdebug } bind def
  136.  
  137. % ---------------- Main program ---------------- %
  138.  
  139. % We process the command line in two passes.  In the first pass,
  140. % we read and expand any @-files as necessary.  The second pass
  141. % does the real work.
  142.  
  143. /cmdstart
  144.  { //cmddict begin
  145.    /expand@ true def
  146.    [
  147.         % Process the GS_OPTIONS environment variable.
  148.    (GS_OPTIONS) getenv { 0 () /SubFileDecode filter expandargfile } if
  149.         % Process the actual command line.
  150.    .getargv { expandarg } forall
  151.    ] readonly /argv exch def
  152.         % Now interpret the commands.
  153.     { nextarg not { exit } if
  154.       dup 0 get (-) 0 get eq
  155.        { dup length 1 eq
  156.       { pop (%stdin) (r) file runjob
  157.       }
  158.       { dup length 2 gt
  159.          { dup dup length 2 sub 2 exch getinterval exch 1 1 getinterval }
  160.         if currentdict .knownget
  161.          { exec
  162.          }
  163.          { (Ignoring unknown switch ) print
  164.            dup length 1 eq { (-) print print } if print
  165.            (\n) print flush
  166.          }
  167.         ifelse
  168.       }
  169.      ifelse
  170.        }
  171.        { runfilejob
  172.        }
  173.       ifelse
  174.     }
  175.    loop end
  176.  } bind def
  177.  
  178. end        % cmddict
  179.